var services = angular.module('services',['ngCookies']);
var filters = angular.module('filters',[]);
var directives = angular.module('directives', ['services']);
var main = angular.module('main', ['directives','services','filters','ngCookies','ngRoute','angularCSS','darthwade.loading']);


main.config(['$routeProvider', function ($routeProvider) {
    console.log('config');
    $routeProvider.when('/dashBroad', {
        templateUrl: 'app/pages/dashBroad/dashBroad.html',
        controller: 'dashBroadCtrl'                  
    }).when('/attr', {
        templateUrl: 'app/pages/attr/attr.html',
        controller: 'attrCtrl'                  
    }).when('/lasty', {
        templateUrl: 'app/pages/lasty/lasty.html',
        controller: 'lastyCtrl'                  
    }).when('/login', {
        templateUrl: 'app/pages/login/login.html',
        controller: 'loginCtrl'                  
    }).when('/reg', {
        templateUrl: 'app/pages/reg/reg.html',
        controller: 'regCtrl'                  
    }).otherwise({
        redirectTo: '/login'
    });
}]);

main.run(['$route', '$templateCache', '$http', (function($route, $templateCache, $http) {
    var url;
    for (var i in $route.routes) {
        if ($route.routes[i].preload) {
            if (url = $route.routes[i].templateUrl) {
                $http.get(url, { cache: $templateCache });
            }
        }
    }
})]);

main.controller('navCtrl', ['$scope','$location','NoticeServ','Session','$rootScope','tools','$timeout', function ($scope,$location,NoticeServ,Session,$rootScope,tools,$timeout) {
    $scope.userInfo = false;

    $scope.pageChange = function(href){
        $location.path(href);
    }

    $(".button-collapse").sideNav();

    

    $scope.userUrlPost = function(){
        tools.get({
            url: '/marketconditions/index/getUser.htm',
            data: {},
            succ:function(resp){
                
                $rootScope.userInfo = resp.value;
                
                console.log('resp:');
                console.log(resp);

                $timeout(function(){
                    $rootScope.$broadcast('GLOBAL_CATEGORY_CHANGE_EV','/');
                },0);
                
            }

        });
    };
    $scope.logout = function(){
        var info = $rootScope.userInfo;
        console.log('login out:');
        console.log(info)
        Session.destroy();
        tools.get({
            url: '/marketconditions/user/userLoginOut.htm',
            succ: function (resp) {
                if(resp.success){
                    $(".button-collapse").sideNav('hide');
                    $location.path('/login');
                }
                
            }
        });
    }

    $scope.userUrlPost();

    NoticeServ.$on($scope,'LOGIN_EV',function(){
        $rootScope.userInfo = Session.UserInfo;
        $rootScope.$broadcast('GLOBAL_CATEGORY_CHANGE_EV','/');
    });
}]);
